home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Development / Source / Incognito / init / nbpaction.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-20  |  5.4 KB  |  300 lines  |  [TEXT/MPS ]

  1. #include <Types.h>
  2. #include <PLStringFuncs.h>
  3. #include <Memory.h>
  4. #include <GestaltEqu.h>
  5. #include <time.h>
  6. #include <errors.h>
  7.  
  8. typedef struct NameLink
  9. {
  10.     Str32 originalName;
  11.     Str32 newString;
  12.     struct NameLink *next, * previous;
  13. } NameLink, * NameLinkPtr;
  14.  
  15. typedef struct OriginalLink
  16. {
  17.     Str32 originalName;
  18.     struct OriginalLink *next;
  19. } OriginalLink, * OriginalLinkPtr;
  20.  
  21. typedef struct
  22. {
  23.     NameLinkPtr registeredNames;
  24.     OriginalLinkPtr trappedNames;
  25. } MyGestaltRecord, * MyGestaltPtr;
  26.  
  27. static MyGestaltPtr GetGestaltInfo(void)
  28. {
  29.     register MyGestaltPtr    pb;
  30.     register OSErr            error;
  31.     
  32.     error = Gestalt('HEID', (long *)&pb);
  33.     if (error) pb = 0;
  34.     return pb;
  35. }
  36.  
  37. static NameLinkPtr GetRegisteredNames(void)
  38. {
  39.     register NameLinkPtr pb = nil;
  40.     register MyGestaltPtr pc;
  41.     
  42.     pc = GetGestaltInfo();
  43.     if (pc)
  44.     {
  45.         pb = pc->registeredNames;
  46.     }
  47.     return pb;
  48. }
  49.  
  50. static OriginalLinkPtr GetMaskableNames(void)
  51. {
  52.     register OriginalLinkPtr    pb = nil;
  53.     register MyGestaltPtr        pc;
  54.     
  55.     pc = GetGestaltInfo();
  56.     if (pc)
  57.     {
  58.         pb = pc->trappedNames;
  59.     }
  60.     return pb;
  61. }
  62.  
  63. /*
  64.   Is the string one that we should mask out?
  65. */
  66.  
  67. static Boolean StringIsMaskable(char* theString)
  68. {
  69.     OriginalLinkPtr pb = GetMaskableNames();
  70.     char* maskString;
  71.     short result;
  72.  
  73.     if (!pb) return false;
  74.     
  75.     maskString = pb->originalName;
  76.  
  77.     while (true && maskString && maskString[0])
  78.     {
  79.         result = !PLstrcmp(theString, maskString);
  80.         if (result)
  81.         {
  82.             return true;
  83.         }
  84.         pb = pb->next;
  85.         if (!pb) break;
  86.         maskString = pb->originalName;
  87.     }
  88.     return false;
  89. }
  90.  
  91. /*
  92.   Has the string already been masked out?
  93. */
  94.  
  95. static Boolean IsStringMasked(char* theString)
  96. {
  97.     NameLinkPtr pb = GetRegisteredNames();
  98.     register char* registeredName;
  99.     short result;
  100.  
  101.     if (!pb) return false;
  102.     registeredName = pb->originalName;
  103.  
  104.     while (true && registeredName && registeredName[0])
  105.     {
  106.         result = !PLstrcmp(theString, registeredName);// check original name
  107.         if (result)
  108.         {
  109.             return true;
  110.         }
  111.         pb = pb->next;
  112.         if (!pb) break;
  113.         registeredName = pb->originalName;
  114.     }
  115.     return false;
  116. }
  117.  
  118. /*
  119.   This is made 4 quark: it tries to deregister a string,
  120.   so if the string is already masked set the string passed
  121.   in to the mask.
  122. */
  123.  
  124. static void SetStringToMask(char* theString)
  125. {
  126.     NameLinkPtr pb = GetRegisteredNames();
  127.     char* registeredName;
  128.     short result;
  129.  
  130.     if (!pb) return;
  131.     registeredName = pb->originalName;
  132.  
  133.     while (true && registeredName && registeredName[0])
  134.     {
  135.         if (registeredName && registeredName[0])
  136.         {
  137.             result = !PLstrcmp(theString, registeredName);    // check original name
  138.         }
  139.         else break;
  140.         if (result)
  141.         {
  142.             PLstrcpy(theString, pb->newString);
  143.             return;
  144.         }
  145.         pb = pb->next;
  146.         if (!pb) break;
  147.         registeredName = pb->originalName;
  148.     }
  149.     return;
  150. }
  151.  
  152. /*
  153.   Munge the string
  154. */
  155.  
  156. static void Munge(char* theString)
  157. {
  158.     register short i;
  159.     register char* j;
  160.     
  161.     j = theString;
  162.     i = theString[0];
  163.     while (i)
  164.     {
  165.         theString[i--] = clock() + i;
  166.     }
  167. }
  168.  
  169. /*
  170.   Insert the old & new strings into the mask list, & munge too
  171. */
  172.  
  173. static void MaskString(char* theString)
  174. {
  175.     NameLinkPtr pb = GetRegisteredNames();
  176.     NameLinkPtr newLink;
  177.     
  178.     if (!pb) return;
  179.  
  180.     newLink = (NameLinkPtr) NewPtrSysClear(sizeof(NameLink));
  181.     if (!newLink) return;
  182.     PLstrcpy(newLink->originalName, theString);
  183.     Munge(theString);
  184.     PLstrcpy(newLink->newString, theString);
  185.  
  186.     while (true)
  187.     {
  188.         if (pb->next)
  189.         {
  190.             pb = pb->next;
  191.         }
  192.         else
  193.         {
  194.             pb->next = newLink;
  195.             newLink->previous = pb;
  196.             return;
  197.         }
  198.     }
  199.  
  200. }
  201.  
  202. /*
  203.   removes an entry from the NameLink list
  204. */
  205.  
  206. static void RemoveEntry(char* theString)
  207. {
  208.     NameLinkPtr pb = GetRegisteredNames();
  209.     char* str;
  210.  
  211.     if (!pb || !theString || !theString[0]) return;
  212.     
  213.     str = pb->newString;
  214.     while (true && str && str[0])
  215.     {
  216.         if (!PLstrcmp(str, theString))
  217.         {
  218.             if (pb->previous)
  219.             {
  220.                 (pb->previous)->next = pb->next;
  221.             }
  222.             if (pb->next)
  223.             {
  224.                 (pb->next)->previous = pb->previous;
  225.             }
  226.             DisposePtr((Ptr)pb);
  227.             return;
  228.         }
  229.         pb = pb->next;
  230.         if (!pb) break;
  231.         str = pb->newString;
  232.     }
  233.  
  234. }
  235.  
  236. // Basically, when registering something, we ask: is the object being deregistered
  237. // one of the ones we should mask out, and if so, has it already been munged?
  238. // If it was already munged, we should set the string to be deregistered to the
  239. // munged string, not the original string.
  240.  
  241. // Why? Because quark, for reasons unknown, registers, throws away the parameter
  242. // block, then deregisters the string that it was supposed to be registered as,
  243. // not the string it actually was registered as (like every other program).
  244.  
  245. void DoRemove(char* typeName, char* objName)
  246. {
  247.     if (typeName && typeName[0])
  248.     {
  249.         if (StringIsMaskable(typeName) && IsStringMasked(typeName))
  250.         {
  251.             SetStringToMask(typeName);
  252.         }
  253.         RemoveEntry(typeName);
  254.     }
  255.     
  256.     if (objName && objName[0])
  257.     {
  258.         if (StringIsMaskable(objName) && IsStringMasked(objName))
  259.         {
  260.             SetStringToMask(objName);
  261.         }
  262.         RemoveEntry(objName);
  263.     }
  264. }
  265.  
  266. static void CheckRegister(char* typeName)
  267. {
  268.     if (!typeName || !typeName[0]) return;
  269.     
  270.     if (StringIsMaskable(typeName))                // should we worry about wiping the type?
  271.     {
  272.         if (!IsStringMasked(typeName))
  273.         {
  274.             MaskString(typeName);
  275.         }
  276.     }
  277. }
  278.  
  279. void DoRegister(char* typeName, char* objName)
  280. {
  281.     if (typeName && typeName[0]) CheckRegister(typeName);
  282.     if (objName && objName[0]) CheckRegister(objName);
  283. }
  284.  
  285. //    Also does lookups, since they're basically the same.
  286. void DoConfirm(char* typeName, char* objName)
  287. {
  288.     if (typeName && typeName[0])
  289.     if (StringIsMaskable(typeName))
  290.     {
  291.         Munge(typeName);
  292.     }
  293.     
  294.     if (objName && objName[0])
  295.     if (StringIsMaskable(objName))
  296.     {
  297.         Munge(objName);
  298.     }
  299. }
  300.